package frontend.components.header;

import java.io.Serializable;

import frontend.pages.worklog.daily.DailyWorkLogPage;
import org.apache.log4j.Logger;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.ResourceModel;

import shared.other.UserRolesDefinition;

import frontend.components.language.LanguagePanel;
import frontend.components.menu.MenuDefinition;
import frontend.components.menu.TreeBasedMenu;
import frontend.core.TimeTrackingWebSession;

@SuppressWarnings("serial")
public class HeaderPanel extends Panel{

    private static Logger logger = Logger.getLogger(HeaderPanel.class);

	public HeaderPanel(String id) {
		super(id);
		
		addLoginInfo();
		addNavigation();
		add(new LanguagePanel("languagePanel"));
	}

	private void addNavigation() {
		TreeBasedMenu navigationMenu = new TreeBasedMenu("nav", MenuDefinition.createMenuDefinition());
		add(navigationMenu);
	}

	private void addLoginInfo() {
		Serializable loginUser = TimeTrackingWebSession.get().getAttribute(
				TimeTrackingWebSession.LOGIN_FULL_NAME_ATTR);
        logger.info("Found on session: user=" + loginUser);
		String loginUserRole = (String) TimeTrackingWebSession.get().getAttribute(
				TimeTrackingWebSession.LOGIN_ROLE_ATTR);
        logger.info("Found on session: role=" + loginUserRole);
		String roleNameKey = UserRolesDefinition.get(loginUserRole).getName();
		add(new Label("loggedInUser", "" + loginUser));
		add(new Label("loggedInUserRole", new ResourceModel(roleNameKey)));
		add(new Link<String>("signoutLink") {
			@Override
			public void onClick() {
				TimeTrackingWebSession.get().invalidate();
                setResponsePage(DailyWorkLogPage.class);
			}
		});
	}
}
